import os 

from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

os.environ["LANGSMITH_TRACING_V2"] = "true"
os.environ["LANGSMITH_API_KEY"] = "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"
os.environ["OPENAI_BASE_URL"] = "https://api.chatanywhere.tech/v1"
os.environ["OPENAI_API_KEY"] = "sk-pbXvhNj37SZ5SUBzC1Kx4LeXrsnT9EJNDL6mT2Lj2IbgohKa"

# 定义提示模板
prompt_template = ChatPromptTemplate.from_messages([
    ("system", "请将以下内容翻译成{language}"),
    ("user", "{text}")
])


if __name__ == "__main__":
    model = ChatOpenAI(model="gpt-4o-mini")
    # msg = [ 
    #     SystemMessage(content="你是一位中文助手"),
    #     HumanMessage(content="请告诉我django支持连接的数据库有哪些？")
    # ]
    parse = StrOutputParser()
    # result = model.invoke(msg)
    chain = prompt_template | model | parse 
    # 使用 chain 调用
    result = chain.invoke({"language": "广东方言", "text": "你好"})
    print(result)
